$(function () {
    let page = document.getElementsByClassName("pagination")[0]
    let pagelist = page.getElementsByTagName("li")
    for (let i = 1; i <= pagelist.length; i++) {
        if (i == page.title) {
            pagelist[i - 1].className = "active"
        }
    }
    let btndanger = document.getElementsByClassName("btn-danger")
    for (let i = 0; i < btndanger.length; i++) {
        btndanger[i].onclick = (e) => {
            e.preventDefault();
            var data = {
                id: btndanger[i].getAttribute('delhref').split('?')[1].split('&')[0].split('=')[1],
                page: btndanger[i].getAttribute('delhref').split('?')[1].split('&')[1].split('=')[1]
            }
            $.ajax({
                url: '/deleteblogs',
                type: 'get',
                data: data,
                dataType: 'json',
                success: (d) => {
                    console.log("success");
                    switch (d.code) {
                        case 1:
                            swal.fire({ title: d.msg, type: "error" })
                            break;
                        case 2:
                            swal.fire({ title: d.msg, type: "success" }).then(() => {
                                window.location.href = "/manageblogs?page= " + data.page
                            })
                            break;
                        case 8:
                            swal.fire({ title: d.msg, type: "error" })
                            break;
                    }
                },
                error: () => {
                    swal.fire({ title: "服务器出错了", type: "error", confirmButtonText: '请重新登陆' }).then(() => {
                        window.location.href = "/login"
                    })
                }
            })
        }
    }
})